package com.example.maggie.meizinote.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.example.maggie.meizinote.app.MApp;
import com.example.maggie.meizinote.app.utils.KeyBoardUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.example.maggie.meizinote.di.component.DaggerLoginComponent;
import com.example.maggie.meizinote.di.module.LoginModule;
import com.example.maggie.meizinote.mvp.contract.LoginContract;
import com.example.maggie.meizinote.mvp.presenter.LoginPresenter;

import com.example.maggie.meizinote.R;


import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.cet_email)
    EditText cet_email;
    @BindView(R.id.et_pwd)
    EditText et_pwd;
    @BindView(R.id.ll_login_root)
    LinearLayout llLoginRoot;
    @BindView(R.id.rel_content)
    RelativeLayout relContent;

    @OnClick({R.id.btn_login, R.id.tv_regis, R.id.tv_forget})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_regis:
                startActivityForResult(new Intent(this, RegisterActivity.class), 1);
                break;
            case R.id.btn_login:
                mPresenter.login(cet_email.getText().toString(), et_pwd.getText().toString());
                break;
            case R.id.tv_forget:
                startActivity(new Intent(this, RegisterActivity.class)
                        .putExtra("isRegiste",false));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == 911) {
                    String username = data.getStringExtra("username");
                    String password = data.getStringExtra("password");
                    mPresenter.login(username, password);
                }
                break;
        }
    }

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerLoginComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_login; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setTitle("登录");
        KeyBoardUtils.keepLoginBtnNotOver(llLoginRoot, relContent);
        //触摸外部，键盘消失
        llLoginRoot.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyBoardUtils.closeKeyboard(LoginActivity.this);
                return false;
            }
        });
        String id = MApp.getUserId();
        if (id != null && !id.trim().isEmpty()) {
            //已登录状态直接进入首页
            mPresenter.toHome();
        }
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


}
